package com.enjoy.basic.serializable;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by HP on 2019/7/15.
 */
public class MsgPackServerHandler extends ChannelInboundHandlerAdapter{
    private AtomicInteger counter=new AtomicInteger(0);
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        User  user= (User) msg;
        System.out.println("server acc:"+user+"counter is:"+counter.incrementAndGet());
        //服务器的应答
        String resp="eminem:"+user.getUserName()+System.getProperty("line.separator");
        ctx.writeAndFlush(Unpooled.copiedBuffer(resp.getBytes()));
        ctx.fireChannelRead(user);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
       cause.printStackTrace();
        ctx.close();
    }
}
